/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof.wget;

import java.net.MalformedURLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Carlos Pinheiro
 */
public class ExecutorParser implements Runnable
{
    private ResizeableSemaphore rs;
    
    public ExecutorParser (ResizeableSemaphore sem) throws InterruptedException
    {
        this.rs = sem;
        this.rs.acquire();
    }
    
    @Override
    public void run()
    {
        WebResource wr = ToParseQueue.getInstance().poll();
        if (wr == null)
        {
            return;
        }

        System.out.println("Parsing " + wr.getLocalPath() + " ...");
        List<String> links = HtmlParser.extractLinks(wr.getSourceUrl().getHost(), wr.getLocalPath());
        for (String link : links)
        {
            try
            {
                WebResource w = new WebResource(link, wr.getLocalPath());
                PendingQueue.getInstance().add(w);
            }
            catch (MalformedURLException ex)
            {
                Logger.getLogger(ExecutorDownloader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        System.out.println("Parsed " + wr.getLocalPath() + ".");
        this.rs.release();
    }     
}
